import { useEffect, useState } from 'react';
import { getLoadMapList, getTenantID, getSelectDataMapList } from '@/utils';
import { ILoadMapTypeAll } from '@/common';
import { useDescriptionsType } from '@/components/DescriptionsType';
import { Form, Select, TreeSelect } from 'antd';
import { warnManageService } from '../services';
import { getFlowOrgTreeByOrgIDService } from '@/services/BaseLayout';
import { handleAllDepartment } from '@/pages/ContractController/ContractManage/mconfig';

const getUpdateConfig = ({ acceptForm, pageType, datalist, storeList, getUserLoading, getUserList, changeGetUser }) => {
  /**  获取数据字典 */
  const { loadMapInfo, mapLoading } = getLoadMapList([
    `${getTenantID()}${ILoadMapTypeAll.MATERIAL_BRAND}`, //物料品牌
  ]);
  /** 物料品牌 */
  const assetBrand = getSelectDataMapList(loadMapInfo?.[`${getTenantID()}${ILoadMapTypeAll.MATERIAL_BRAND}`], 'name', 'id', true);

  /** 获取组织机构树接口 */
  const { getFlowOrgTreeRun, getFlowOrgTreeLoading, getFlowOrgList } = getFlowOrgTreeByOrgIDService();
  /** 表单数据 */

  /** 切换表单 */
  useEffect(() => {
    /** 初始化请求组织机构 */
    let payload = {
      params: '0',
      flowType: '0',
    };
    getFlowOrgTreeRun(payload);
    changeGetUser(datalist?.storeCompanyID);
    acceptForm.setFieldsValue(datalist);
  }, []);

  /** 表单配置 */
  const [config, methods] = useDescriptionsType({
    labelWidth: 190,
    basicForm: acceptForm,
    name: 'accept-addAndEdit',
    schemas: [
      {
        key: 'accept-basic',
        title: '',
        fieldChildren: [
          // {
          //   field: `storeCode`,
          //   label: `仓库编码`,
          //   component: 'InputTextArea',
          //   required: true,
          //   placeholder: '请输入',
          //   autoSize: { minRows: 1, maxRows: 3 },
          // },
          {
            field: `storeName`,
            label: `仓库名称`,
            component: 'Input',
            disabled: pageType === 'view',
            placeholder: '请输入',
            required: true,
          },
          // {
          //   field: `storeAlias`,
          //   label: `仓库别名`,
          //   component: 'Input',
          //   disabled: pageType === 'view',
          //   required: true,
          //   placeholder: '请选择',
          // },
          // {
          //   field: `storeNum`,
          //   label: `仓库编号`,
          //   component: 'Input',
          //   disabled: pageType === 'view',
          //   required: true,
          //   placeholder: '请输入',
          // },
          // {
          //   field: `storeState`,
          //   label: `仓库状态`,
          //   component: 'Input',
          //   disabled: pageType === 'view',
          //   required: true,
          //   placeholder: '请输入',
          // },
          // {
          //   field: `storeDetail`,
          //   label: `仓库说明`,
          //   component: 'Input',
          //   disabled: pageType === 'view',
          //   required: true,
          //   placeholder: '请输入',
          // },
          {
            field: `pId`,
            label: `上级仓库`,
            component: 'Render',
            placeholder: '请输入',
            render: (text) => {
              return (
                <TreeSelect
                  // multiple
                  placeholder="请选择"
                  style={{
                    width: '100%',
                  }}
                  dropdownStyle={{
                    maxHeight: 400,
                    overflow: 'auto',
                  }}
                  fieldNames={{
                    label: 'storeName',
                    value: 'id',
                    children: 'children',
                  }}
                  treeData={storeList}
                  // treeCheckable={true}
                />
              );
            },
          },
          {
            field: `storePlace`,
            label: `仓库位置`,
            component: 'Input',
            disabled: pageType === 'view',
            required: true,
            placeholder: '请输入',
          },
          {
            field: `storeCompanyID`,
            label: `仓库所属单位`,
            disabled: pageType === 'view',
            component: 'Render',
            required: true,
            render: (text) => {
              return (
                <TreeSelect
                  loading={getFlowOrgTreeLoading}
                  placeholder="请选择"
                  treeData={handleAllDepartment(getFlowOrgList)}
                  disabled={pageType == 'view' ? true : false}
                  showArrow={pageType == 'view' ? false : true}
                  fieldNames={{
                    label: 'title',
                    value: 'id',
                    children: 'children',
                  }}
                  showSearch={true}
                  treeNodeFilterProp={'text'}
                  allowClear={true}
                  onChange={(value: string | number) => {
                    changeGetUser(value);
                  }}
                />
              );
            },
          },
          {
            field: `storeUserID`,
            label: `仓库管理员`,
            disabled: pageType === 'view',
            component: 'Render',
            required: true,
            // colSpan: 2,
            render: (text) => {
              if (pageType === 'view') {
                return <span>{datalist.storeDepartmentName || '--'}</span>;
              } else {
                return (
                  <Select
                    showSearch
                    loading={getUserLoading}
                    options={getUserList}
                    disabled={pageType === 'view'}
                    placeholder="请选择"
                    fieldNames={{
                      label: 'realName',
                      value: 'id',
                    }}
                    optionFilterProp="children"
                    filterOption={(input, option) => (option?.realName ?? '').includes(input)}
                    filterSort={(optionA, optionB) => (optionA?.realName ?? '').toLowerCase().localeCompare((optionB?.realName ?? '').toLowerCase())}
                    onChange={(value: number | string, option: any) => {
                      // getSelectUserInfo(value, option);
                      acceptForm.setFieldsValue({
                        storeDutyPhone: option.phoneMobile,
                      });
                    }}
                  />
                );
              }
            },
          },
          // {
          //   field: `storeUnit`,
          //   label: `仓库所属单位`,
          //   component: 'Input',
          //   disabled: pageType === 'view',
          //   required: true,
          //   placeholder: '请输入',
          // },
          // {
          //   field: `storeDutyUser`,
          //   label: `仓库管理员`,
          //   component: 'Input',
          //   disabled: pageType === 'view',
          //   required: true,
          //   placeholder: '请输入',
          // },
          {
            field: `storeDutyPhone`,
            label: `仓库管理员电话`,
            component: 'Input',
            disabled: pageType === 'view',
            required: true,
            placeholder: '请输入',
          },
        ],
      },
    ],
  });

  return {
    config,
    methods,
  };
};

export { getUpdateConfig };
